//
//  CalendarExampleViewController.m
//  CalendarExample
//

#import "CalendarExampleViewController.h"

@implementation CalendarExampleViewController

- (void)logCalendarEventsForNextMonth
{
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    
    NSDate *startDate = [NSDate date];
    
    // 1개월 이후의 날짜를 찾는다.
    NSDateComponents *comp = [[NSDateComponents alloc] init];
    [comp setMonth:1];
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *endDate = [cal dateByAddingComponents:comp toDate:startDate options:0];
    
    // 오늘부터 1개월 간의 날짜 조건을 생성한다.
    NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate
																 endDate:endDate
															   calendars:nil];
    NSArray *matchingEvents = [eventStore eventsMatchingPredicate:predicate];
    
    for (EKEvent *e in matchingEvents)
    {
		NSLog(@"-----");
		NSLog(@"EventIdentifier: %@)",e.eventIdentifier);
		NSLog(@"Title: %@",e.title);
		NSLog(@"Start: %@",e.startDate);
		NSLog(@"End: %@",e.endDate);
		NSLog(@"Location: %@",e.location);
		NSLog(@"Notes: %@",e.notes);
    }
    
    [cal release];
    [comp release];
    [eventStore release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	[self logCalendarEventsForNextMonth];
}

- (void)dealloc {
    [super dealloc];
}

@end
